/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class code4 {
    //二叉树的最大直径（不一定经过root)
    int ret;
    public int diameterOfBinaryTree(TreeNode root) {
        MaxHeight(root);
        return ret;
    }

    public int MaxHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int LH = MaxHeight(root.left);
        int LR = MaxHeight(root.right);
        ret = Math.max(ret,LH+LR);
        return Math.max(LH,LR)+1;
    }
}